/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.room.analyse;

import log.Debug;

/**
 *
 * @author daz
 */
public class Extract {

    private static void extractFlush(int[] listCards) {
        int numberCards = listCards.length;
        for (int i = 0; i < numberCards; i++) {
            for (int j = i + 1; j < numberCards; j++) {
                if (listCards[i] / 13 > listCards[j] / 13
                        || (listCards[i] / 13 == listCards[j] / 13
                        && listCards[i] % 13 > listCards[j] % 13)) {
                    swap(i, j, listCards);
                }
            }
        }
        for (int i = 0; i < numberCards; i++) {
            int step = 0;
            int cardId = listCards[i];
            for (int j = i + 1; j < numberCards; j++) {
                if (cardId + (j - i) == listCards[j] && cardId / 13 == listCards[j] / 13) {
                    step++;
                } else {
                    j = numberCards;
                }
            }
            if (step != 0) {
                Debug.d("day :" + step);
                for (int k = i; k <= i + step; k++) {
                    Debug.n(Card.getRank(listCards[k]) + Card.getSuit(listCards[k]));
                }
                Debug.d("");
                i = i + step;
            }

        }
    }

    private static void swap(int i, int j, int[] listCards) {
        int temp = listCards[j];
        listCards[j] = listCards[i];
        listCards[i] = temp;
    }

    private static void extractPair(int[] listCards) {
        int numberCards = listCards.length;
        for (int i = 0; i < numberCards; i++) {
            for (int j = i + 1; j < numberCards; j++) {
                if (listCards[i] % 13 > listCards[j] % 13) {
                    int temp = listCards[j];
                    listCards[j] = listCards[i];
                    listCards[i] = temp;
                }
            }
        }
        //tim phom ngang
        for (int i = 0; i < numberCards; i++) {
            int cardId = listCards[i];
            int step = 0;
            for (int j = i + 1; j < numberCards; j++) {
                if (cardId % 13 == listCards[j] % 13) {
                    if (j < numberCards - 1 && cardId % 13 == listCards[j + 1] % 13) {
                        if (j < numberCards - 2 && cardId % 13 == listCards[j + 2]) {
                            Debug.d("bo bon");
                            step = 3;
                        } else {
                            Debug.d("bo ba");
                            step = 2;
                        }
                    } else {
                        Debug.d("bo 2");
                        step = 1;
                    }
                    for (int k = i; k <= i + step; k++) {
                        Debug.n(Card.getRank(listCards[k]) + Card.getSuit(listCards[k]));
                    }
                    Debug.d("");
                    j = numberCards;
                    i = i + step + 1;
                }
            }
        }
    }

    private static void printCard(int[] listCards) {
        for (Integer i : listCards) {
            Debug.n(Card.getRank(i) + Card.getSuit(i));
        }
        System.out.println();
    }

    private boolean checkPair(int[] listCards) {
        int numberCards = listCards.length;
        for (int i = 0; i < numberCards; i++) {
            for (int j = i + 1; j < numberCards; j++) {
                if (listCards[i] % 13 > listCards[j] % 13) {
                    int temp = listCards[j];
                    listCards[j] = listCards[i];
                    listCards[i] = temp;
                }
            }
        }

        for (int i = 0; i < numberCards; i++) {
            int cardId = listCards[i];
            int step = 0;
            for (int j = i + 1; j < numberCards; j++) {
                if (cardId % 13 == listCards[j] % 13) {
                    if (j < numberCards - 1 && cardId % 13 == listCards[j + 1] % 13) {
                        if (j < numberCards - 2 && cardId % 13 == listCards[j + 2] % 13) {
//                            Debug.d("bo bon");
                            step = 3;
                        } else {
//                            Debug.d("bo ba");
                            step = 2;
                        }
                    } else {
//                        Debug.d("bo 2");
                        step = 1;
                    }
                    int[] pair = new int[step + 1];
                    for (int k = i; k <= i + step; k++) {
                        pair[k - i] = listCards[k];
//                        Debug.n(Card.getRank(listCards[k]) + Card.getSuit(listCards[k]));
                    }
                    if (step >= 2) {
                        return true;
                    }
//                    Debug.d("");
                    j = numberCards;
                    i = i + step;
                }
            }
        }
        return false;
    }

    private boolean checkFlush(int[] listCards) {
        int numberCards = listCards.length;
        for (int i = 0; i < numberCards; i++) {
            for (int j = i + 1; j < numberCards; j++) {
                if (listCards[i] / 13 > listCards[j] / 13
                        || (listCards[i] / 13 == listCards[j] / 13
                        && listCards[i] % 13 > listCards[j] % 13)) {
                    swap(i, j, listCards);
                }
            }
        }
        for (int i = 0; i < numberCards; i++) {
            int step = 0;
            int cardId = listCards[i];
            for (int j = i + 1; j < numberCards; j++) {
                if (cardId + (j - i) == listCards[j] && cardId / 13 == listCards[j] / 13) {
                    step++;
                } else {
                    j = numberCards;
                }
            }
            if (step != 0) {
//                Debug.d("day :" + step);
                int[] flsush = new int[step + 1];
                for (int k = i; k <= i + step; k++) {
                    flsush[k - i] = listCards[k];
//                    Debug.n(Card.getRank(listCards[k]) + Card.getSuit(listCards[k]));
                }
                if (step >= 2) {
                    return true;
                }
                i = i + step;
            }
        }
        return false;
    }
    
}
